<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    var socket;
    //判断是否支持ws
    if (window.WebSocket) {
        socket = new WebSocket("ws://localhost:8888/hello");
        socket.onmessage = function (ev) {
            var msg = document.getElementById("responseText");
            msg.value = msg.value + "\n" + ev.data;
        }
        socket.onopen = function (ev) {
            var msg = document.getElementById("responseText");
            msg.value = "链接开启.....";
        }
        socket.onclose = function (ev) {
            var msg = document.getElementById("responseText");
            msg.value = msg.value + "\n" + "链接关闭.....";
        }

    } else {
        alert("当前浏览器不支持WebSocket")
    }

    function send(message) {
        if (!window.socket) {
            return;
        }
        if (socket.readyState == WebSocket.OPEN) {
            socket.send(message);
        } else {
            alert("链接未开启");
        }
    }
</script>
<form onsubmit="return false">
    <textarea name="message" style="height: 300px;width: 300px"></textarea>
    <input type="button" value="发送消息" onclick="send(this.form.message.value)">
    <textarea id="responseText" style="height: 300px;width: 300px"></textarea>
    <input type="button" value="清空消息" onclick="document.getElementById('responseText').value =''">
</form>

</body>
</html>